package ThreadPoolDemo.bilibiliThread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TestDemo {
	public static void main(String[] args) {
		// 添加线程定时器
		Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				// 添加jdbc
				try {
					while (ThreadTarget.getTaskNum > 0
							&& ThreadTarget.getTaskNum > ThreadTarget.overTaskNum) {
						Thread.sleep(5000);
					}
					ThreadTarget.getTaskNum = 0;
					ThreadTarget.overTaskNum = 0;
					Class.forName("com.mysql.jdbc.Driver");
					Connection conn = DriverManager.getConnection(
							"jdbc:mysql://127.0.0.1:3306/micro_message",
							"root", "a709796558");
					StringBuilder sql = new StringBuilder(
							"select id, branch_id from tb_branch");
					PreparedStatement statement = conn.prepareStatement(sql
							.toString());
					ResultSet rs = statement.executeQuery();
					while (rs.next()) {
						String branchId = rs.getString("branch_id");
						String id = rs.getString("id");
						// 获取单线程线程池
						ExecutorService slginThreadPool = ThreadTarget
								.getinstance().getThreadPool(branchId);
						slginThreadPool.execute(new task(branchId + "-" + id));
						ThreadTarget.getTaskNum++;
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}, 1, 5, TimeUnit.SECONDS);
	}
}

class ThreadTarget {
	private ThreadTarget() {
	};

	private static ThreadTarget obj = new ThreadTarget();

	public static ThreadTarget getinstance() {
		return obj;
	}

	private static Map<String, ExecutorService> map = new HashMap<String, ExecutorService>();

	public ExecutorService getThreadPool(String str) {
		// 主线程进来
		ExecutorService theFixedThreadPool = map.get(str);
		if (null != theFixedThreadPool) {
			return theFixedThreadPool;
		} else {
			ExecutorService newFixedThreadPool = Executors
					.newSingleThreadExecutor();
			map.put(str, newFixedThreadPool);
			return newFixedThreadPool;
		}
	}

	public static int getTaskNum = 0;

	public static int overTaskNum = 0;
}

class task implements Runnable {
	private String name;

	public task(String name) {
		this.name = name;
	}

	@Override
	public void run() {

		System.out.println(Thread.currentThread().getName()
				+ "----------------开始" + this.getName());
		System.out.println(Thread.currentThread().getName() + "处理任务的分公司为"
				+ this.getName());
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()
				+ "----------------结束" + this.getName());

		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/micro_message", "root",
					"a709796558");
			StringBuilder sql = new StringBuilder(
					"delete from tb_branch where id="
							+ this.getName().split("-")[1]);
			System.out.println(sql.toString());
			PreparedStatement statement = conn.prepareStatement(sql.toString());
			statement.execute();
			System.out.println(Thread.currentThread().getName()
					+ "----------------删除" + this.getName());
		} catch (Exception e) {

			e.printStackTrace();
		} finally {
			ThreadTarget.overTaskNum++;
		}

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}